﻿using System;

namespace  FileData.Currency.TypeConvert
{
    /// <summary>
    /// 工具类：文件与常用进制的转换
    /// </summary>
    public class HexConvert
    {
        /// <summary>
        /// 文件直接转16进制字符串
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public string FileToHex(string FilePath)
        {
            return ByteToHexStr(new ByteConvert().FileToBytes(FilePath));
        }
        /// <summary>
        /// string转换为十六进制字符串
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public string StrToHex(string Str)
        {

            return ByteToHexStr(new ByteConvert().StrTobyte(Str));
        }
        /// <summary>
        /// 十六进制字符串还原为string
        /// </summary>
        /// <param name="HexString"></param>
        /// <returns></returns>
        public string HexStrToStrinig(String HexString)
        {

            return new ByteConvert().ByteToStr(HexStrTobyte(HexString));
        }
        /// <summary>
        /// 十六进文本制转byte
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public byte[] HexStrTobyte(string hexString)
        {
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
            return returnBytes;
        }

        /// <summary>
        /// byte转十六进制
        /// </summary>
        /// <param name="Bytearray"></param>
        /// <returns></returns>
        public string ByteToHexStr(byte[] Bytearray)
        {
            var bytes = Bytearray;
            var hex = BitConverter.ToString(bytes, 0).Replace("-", string.Empty).ToLower();
            return hex;
        }
    }
}